package com.hn658.base.message.common.utils;

/**
 * Created by baihai on 2015/8/24.
 */
public class RedisKeyHelper {

    private static final String keyPrefix = "base:message:";

    /**
     * 根据appKey Key
     * @param appKey
     * @return
     */
    public static String appKey(String appKey) {
        return keyPrefix+"appInfo:appKey:"+appKey;
    }
    
    /**
     * 根据app id
     * @param id
     * @return
     */
    public static String appInfoId(String id) {
        return keyPrefix+"appInfo:id:"+id+":Info";
    }

    /**
     * 数据字典生成key值， 由传入的type动态生成
     * @param type
     * @return
     */
    public static String dictionaryKey(String type){
        return keyPrefix+"dictionary:"+type+"type";
    }
    
    /**
	 * 获取对应userId的用户信息key
	 * @param userId
	 * @return
	 */
	public static String accountId(String accountId) {
		return keyPrefix+"accountId:" + accountId + ":accountInfo";
	}

	/**
	 * 
	 * <p>通过访问令牌信息，获得访问用户ID</p> 
	 * @author ztjie
	 * @date 2015-11-12 下午3:14:24
	 * @param accessToken
	 * @return
	 * @see
	 */
	public static String accountIdByAccessToken(String accessToken) {
		return keyPrefix+"accessToken:" + accessToken;
	}

	/**
	 * 
	 * <p>通过访问用户ID，获得令牌信息</p> 
	 * @author ztjie
	 * @date 2015-11-12 下午3:14:24
	 * @param accessToken
	 * @return
	 * @see
	 */
	public static String accessTokenByAccountId(String accountId) {
		return keyPrefix+"accountId:" + accountId;
	}

	/**
     * 根据Mq id
     * @param id
     * @return
     */
    public static String appMqId(String id) {
        return keyPrefix+"appMq:id:"+id;
    }
    
    /**
     * 根据Mq queueNumber获取对应的id
     * @param queueNumber
     * @return
     */
    public static String appMqNum(String queueNumber) {
        return keyPrefix+"appMq:queueNumber:"+queueNumber+":Number";
    }
    
    /**
     * 根据appKey,userId获取redis中对应的token
     * @param appKey,userId
     * @return
     */
    public static String tokenByAppUid(String appKey, String userId){
    	return keyPrefix+"appKey:"+appKey+":userId:"+userId+":token";
    }
    
    /**
     * 根据token获取redis中对应的appUid
     */
    public static String appUidByToken(String token){
    	return keyPrefix+"token:"+token+":appUid";
    }
    
    /**
     * 根据appKey, uid获取redis中对应的用户信息
     */
    public static String accountInfo(String appKey, String userId){
    	return keyPrefix+"appKey:"+appKey+":userId:"+userId+":userInfo";
    }
    
    /**
     * 用户上次活跃时间戳
     * @return
     */
    public static String lastActiveDateTime() {
        return keyPrefix+"lastActiveDataTime";
    }
    
    /**
     * 缓存群信息
     * @param appKey
     * @param groupId
     * @return
     */
    public static String groupKeyById(String appKey,Long groupId){
    	return keyPrefix+"appKey:"+appKey+":groupId:"+groupId;
    }
    
    /**
     * 缓存用户当前聊天对象信息
     * @param appKey
     * @param uid
     * @return
     */
    public static String talkerKeyById(String appKey,String userId){
    	return keyPrefix+"appKey:"+appKey+":talker:userId:"+userId;
    }
    
    /**
     * 缓存用户未读消息
     * @param appKey
     * @param userId
     * @return
     */
    public static String readCountKeyById(String appKey,String userId){
    	return keyPrefix+"appKey:"+appKey+":readCount:userId:"+userId;
    }

	public static String marshal(String appKey, String userId) {
		return appKey+"_"+userId;
	}
	
	public static String[] unMarshal(String appKeyUserId) {
		return appKeyUserId.split("_");
	}
	 /**
		 * 获取对应userId的后台用户信息key
		 * 
		 * @param userId
		 * @return
		 */
		public static String backendUserId(String userId) {
			return keyPrefix+"uid:" + userId + ":userInfo";
		}
		
		/**
		 * 获取对应后台帐号的userId
		 * 
		 * @param userLoginAccount
		 * @return
		 */
		public static String backendUserAccount(String userLoginAccount) {
			return keyPrefix+"userLoginAccount:" + userLoginAccount + ":userId";
		}
		
		/**
		 * 获取userId对应的auth
		 * 
		 * @param userId
		 * @return
		 */
		public static String backendUserAuth(String userId) {
			return keyPrefix+"userId:" + userId + ":auth";
		}
		
		/**
		 * 获取userAuthKey对应的userId
		 * 
		 * @param userAuthKey
		 * @return
		 */
		public static String backendUserAuthKey(String userAuthKey) {
			return keyPrefix+"auth:" + userAuthKey + ":userId";
		}
		
		/**
		 * 获取accountId对应的auth
		 * 
		 * @param accountId
		 * @return
		 */
		public static String backendAccountAuth(String accountId) {
			return keyPrefix+"accountId:" + accountId + ":auth";
		}

	    /**
	     * 数据字典生成key值， 由传入的type动态生成
	     * @param type
	     * @return
	     */
	    public static String backendDictionaryKey(String type){
	        return keyPrefix+"dictionary:"+type+"type";
	    }
	    
	    /**
		 * 获取对应uid的用户信息key
		 * @param uid
		 * @return
		 */
		public static String backendUid(String uid) {
			return keyPrefix+"uid:" + uid + ":userInfo";
		}
		
		/**
		 * 获取对应帐号的uid
		 * @param loginAccount
		 * @return
		 */
		public static String backendAccount(String loginAccount){
			return keyPrefix+"loginAccount:" + loginAccount + ":uid";
		}
		
		/**
		 * 获取uid对应的auth
		 * @param uid
		 * @return
		 */
		public static String backendAuth(String uid){
			return keyPrefix+"uid:" + uid + ":auth";
		}
		
		/**
		 * 获取authkey对应的用户id
		 * @param authkey
		 * @return
		 */
		public static String backendAuthkey(String authkey){
			return keyPrefix+"auth:" + authkey + ":uid";
		}
		
		/**
		 * 根据tokenId获取账户信息
		 * @return
		 */
		public static String backendTokenAccount(String tokenId){
			return keyPrefix+"tokenId:" + tokenId + ":loginAccount";
		}

		/**
		 * 
		 * <p>权限信息ID缓存KEY</p> 
		 * @author ztjie
		 * @date 2015-11-5 下午3:46:38
		 * @param id
		 * @return
		 * @see
		 */
		public static String functionId(String id) {
			return keyPrefix+"function:id"+id+":info";
		}
		/**
		 * 
		 * <p>查询所有权限树信息</p> 
		 * @author ztjie
		 * @date 2015-11-5 下午3:46:38
		 * @param id
		 * @return
		 * @see
		 */
		public static String allTreeNode() {
			return keyPrefix+"function:allTreeNode";
		}
		/**
		 * 
		 * <p>权限信息uri缓存</p> 
		 * @author ztjie
		 * @date 2015-11-5 下午3:47:44
		 * @param uri
		 * @return
		 * @see
		 */
		public static String functionUri(String uri) {
			return keyPrefix+"function:functionUri:"+uri;
		}

		public static String backendUserAccessTreeNodes(Long uid) {
			return keyPrefix+"auth:treeNode:"+uid;
		}

		public static String backendUserAccessUris(Long uid) {
			return keyPrefix+"auth:accessUris:"+uid;
		}

		public static String backendUserAccessIds(Long uid) {
			return keyPrefix+"auth:accessIds:"+uid;
		}

}
